home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-04 | 10.1 KB | 270 lines | [TEXT/MMCC] |
- //
- // CTCPStream.h
- //
- // TurboTCP library
- // TCP stream class
- //
- // Copyright © 1993-95, FrostByte Design / Eric Scouten
- //
-
- #pragma once
-
- #include "TurboTCP.buildflags.h"
- #include "CTCPDriver.h"
- #include "CTCPEndpoint.h"
-
- #if TurboTCP_UH2
- #include <MacTCP.h>
- #else
- #include <MacTCPCommonTypes.h>
- #include <TCPPB.h>
- #endif
-
- class CTCPAsyncCall;
-
-
- //***********************************************************
- //
- // connection state codes for TCP Status call
- //
-
- enum {
- strClosed = 0,
- strListen = 2,
- strSYNReceived = 4,
- strSYNSent = 6,
- strEstablished = 8,
- strFINWait1 = 10,
- strFINWait2 = 12,
- strCloseWait = 14,
- strClosing = 16,
- strLastAck = 18,
- strTimeWait = 20
- };
-
-
- //***********************************************************
-
- class CTCPStream {
-
- // This class implements a stream for MacTCP. A TCP stream supports one connection at
- // a time; but a connection may be closed and another connection opened without releasing
- // the stream.
-
- // All MacTCP commands are originated through this class. (See the methods labelled
- // “basic user TCP calls” below.) These methods originate asynchronous calls to the
- // MacTCP driver by creating CTCPAsyncCall objects to correspond with each call. When
- // the call is completed, notification is passed back to the caller through the Handle…
- // methods in CTCPEndpoint.
-
- // This object also receives notification of asynchronous events related to the stream and
- // passes these notifications to its dependents through the BroadcastChange mechanism.
- // (See the description of the asynchronous notification routine [ASR] in the MacTCP
- // manuals.)
-
- // Your application class need not be concerned with interrupt-level constraints on
- // memory management. The CTCPStream object queues notifications received at interrupt
- // level and the CTCPDriver schedules their processing at the next event loop.
-
- // TurboTCP 1.0 NOTE: This class is no longer a descendant of CCollaborator. It now
- // communicates only with the CTCPEndpoint mix-in class.
-
- friend class CTCPAsyncCall;
- friend class CTCPDriver;
- friend class CTCPEndpoint;
- friend class CTCPStreamList;
-
-
- // misc constants & types
-
- public:
- enum {
- autoReceiveMax = 32, // maximum # of entries in RDS
- IPOptionStringSize = 40
- };
- typedef char IPOptionString[IPOptionStringSize];
-
-
- // member functions
-
- private:
- CTCPStream(CTCPEndpoint& theEndpoint,
- long recBufferSize = recReceiveSize,
- short autoReceiveSize = recAutoRecSize,
- short autoReceiveNum = recAutoRecNum);
- virtual ~CTCPStream() {} // use Dispose() instead, DO NOT OVERRIDE
- public:
- void Dispose();
-
-
- // basic user TCP calls
-
- void OpenConnection(Boolean passive, ip_addr theRemoteIP, b_16 theRemotePort,
- b_16 theLocalPort);
- void Close();
- void Abort();
-
- void NoCopyRcv(rdsEntry* itsRDS, b_16 itsRDSSize, b_16 itsTimeOut);
- void BfrReturn(Ptr itsRDS);
- void Send(wdsEntry* itsWDS, b_16 itsTimeOut, Boolean disposeWDS,
- Boolean notifyWhenDone);
- void Receive(Ptr theData, b_16 itsDataSize, b_16 itsTimeOut);
- void Status(TCPStatusPB* theStatusBlock);
- b_16 ConnectionState();
- inline Boolean RcvUrgentStatus()
- { return rcvUrgent; }
-
-
- // specialized functions for sending data
-
- void SendBfrCpy(const void* theData, unsigned short theDataSize);
- void SendBfrNoCpy(const void* theData, unsigned short theDataSize,
- Boolean disposeWhenDone, Boolean notifyWhenDone);
- inline void SetNextPush()
- { sendNextPush = true; }
- inline void SetNextUrgent(Boolean useRFC793)
- { sendNextUrgent = (useRFC793 ? 2 : 1); }
-
-
- // set configuration — use these before opening a connection
-
- inline void SetULPTimeoutValue(b_16 ulpTimeoutValue)
- { itsULPtimeout = ulpTimeoutValue; itsValidityFlag |= timeoutValue; }
- inline void SetULPTimeoutAction(b_16 ulpTimeoutAction)
- { itsULPaction = ulpTimeoutAction; itsValidityFlag |= timeoutAction; }
- inline void SetCommandTimeout(b_16 cmdTimeoutValue)
- { itsCommandTimeoutValue = cmdTimeoutValue; }
- inline void SetTypeOfService(b_16 newTosFlag)
- { itsTosFlags = newTosFlag; itsValidityFlag |= typeOfService; }
- inline void SetPrecedence(b_16 newPrecedence)
- { itsPrecedence = newPrecedence; itsValidityFlag |= precedence; }
- inline void SetDontFrag(b_16 newDontFrag)
- { itsDontFrag = newDontFrag; }
- inline void SetTimeToLive(b_16 newTimeToLive)
- { itsTimeToLive = newTimeToLive; }
- inline void SetSecurity(b_16 newSecurity)
- { itsSecurity = newSecurity; }
- inline void SetIPOptions(b_16 newOptionsSize, IPOptionString* newOptions)
- { itsOptionCnt = newOptionsSize; BlockMove(newOptions, &itsOptions, IPOptionStringSize); }
-
-
- // notification routines — used by CTCPAsyncCall and CTCPStream *only*
-
- private:
- inline void HandleTCPError(OSErr theResultCode, short theCsCode)
- { if (itsEndpoint) itsEndpoint->HandleTCPError(theResultCode, theCsCode); }
- inline void HandleClosed()
- { if (itsEndpoint) itsEndpoint->HandleClosed(); }
- inline void HandleClosing(Boolean remoteClosing)
- { if (itsEndpoint) itsEndpoint->HandleClosing(remoteClosing); }
- inline void HandleDataArrived(Ptr theData, b_16 theDataSize, Boolean isUrgent)
- { if (itsEndpoint) itsEndpoint->HandleDataArrived(theData, theDataSize, isUrgent); }
- void HandleDataSent(wdsEntry* WDSPtr, Boolean disposeWhenDone, Boolean notifyWhenDone);
- inline void HandleICMP(struct ICMPReport* icmpMsg)
- { if (itsEndpoint) itsEndpoint->HandleICMP((ICMPType) icmpMsg->reportType,
- icmpMsg->optionalAddlInfo, (void*) (icmpMsg->optionalAddlInfoPtr)); }
- inline void HandleOpened()
- { hasSessionOpen = true; pendingOpen = false; if (itsEndpoint) itsEndpoint->HandleOpened(); }
- inline void HandleOpenFailed(OSErr theResultCode)
- { hasSessionOpen = false; pendingOpen = false;
- if (itsEndpoint) itsEndpoint->HandleOpenFailed(theResultCode); }
- void HandleSendFailed(wdsEntry* WDSPtr, Boolean disposeWhenDone,
- Boolean notifyWhenDone, OSErr theResultCode);
- inline void HandleTerminated(b_16 terminReason)
- { if (itsEndpoint) itsEndpoint->HandleTerminated((TCPTerminReason) terminReason,
- pendingDispose || disposeOnTerminate); }
- inline void HandleTimeout()
- { if (itsEndpoint) itsEndpoint->HandleTimeout(); }
- inline void HandleUnexpectedData()
- { if (itsEndpoint) itsEndpoint->HandleUnexpectedData(); }
- inline void HandleUrgentBegin()
- { if (itsEndpoint) itsEndpoint->HandleUrgentBegin(); }
-
- inline void RcvUrgentBegin()
- { rcvUrgent = true; }
- inline void RcvUrgentMark()
- { rcvUrgent = false; }
-
-
- // private methods for initiating TCP calls
-
- OSErr DoAsyncCall(b_16 theCsCode, TCPiopb* theParamBlock);
- OSErr DoSyncCall(b_16 theCsCode, TCPiopb* theParamBlock);
- void StartAutoReceive()
- { for (short i=1; i<=itsAutoReceiveNum; i++) IssueAutoReceive(); }
- void IssueAutoReceive();
- void ProcessNotify();
- void ProcessAsyncCompletion(CTCPAsyncCall* theCall);
-
-
- // interrupt-level methods: delay processing for non-interrupt status
-
- void PostponeDispose();
- void PostponeNotify(b_16 eventCode, b_16 terminReason, struct ICMPReport* icmpMsg);
- static pascal void NotifyProc(StreamPtr tcpStream, unsigned short eventCode,
- Ptr userDataPtr, unsigned short terminReason,
- struct ICMPReport* icmpMsg);
-
-
-
- public:
- ip_addr itsRemoteIP; // remote host’s IP address
- b_16 itsRemotePort; // remote host’s TCP port
- ip_addr itsLocalIP; // local host’s IP address
- b_16 itsLocalPort; // local host’s TCP port
- Boolean rcvUrgent; // stream is receiving urgent data
- Boolean hasSessionOpen; // currently open session
-
- private:
- CTCPEndpoint* itsEndpoint; // endpoint object for this stream
- QHdr itsAsyncCalls; // list of outstanding async calls
- Ptr macTCPStream; // MacTCP’s reference code for this stream
- Handle itsBuffer; // receive buffer area
- long itsBufferSize; // size of receive buffer
-
- b_16 sendNextUrgent; // next send operation is “urgent” data
- Boolean sendNextPush; // next send operation is “push” data
-
- Boolean notifClosing; // ASR got closing event
- Boolean remoteClose; // ASR close due to remote host
- Boolean notifTimeout; // ASR got ULP timeout event
- Boolean notifTerminate; // ASR got terminate event
- b_16 notifTermReason; // ASR’s termination reason
- Boolean notifDataArrived; // ASR got data w/ no receive
- Boolean notifUrgent; // ASR got urgent data event
- Boolean notifICMP; // ASR got ICMP report
- ICMPReport notifICMPreport; // most recent ICMP report
-
- char itsULPtimeout; // ULP timeout value in seconds
- char itsULPaction; // what to do on ULP timeout
- char itsValidityFlag; // validity bits for optional parms
- char itsCommandTimeoutValue; // command timeout value in seconds
- char itsTosFlags; // type of service flags
- char itsPrecedence; // precedence level
- char itsDontFrag; // don’t fragment flag
- char itsTimeToLive; // time to live
- char itsSecurity; // security flag
- char itsOptionCnt; // count of options bytes
- char itsOptions[IPOptionStringSize]; // IP options
-
- Boolean pendingOpen; // session is being opened
- Boolean pendingClose; // session is being closed
- Boolean pendingAbort; // session is being aborted
- Boolean pendingNotify; // stream is in ProcessNotify queue
- Boolean pendingDispose; // stream is in ProcessDispose queue
- Boolean disposeOnTerminate; // dispose stream when terminate notification comes
- Boolean receivedClose; // received notification that session was closed or aborted
- Boolean receivedTerminate; // terminate notification has been received
-
- short itsAutoReceiveSize; // auto-receive size (# of entries)
- short itsAutoReceiveNum; // number of simultaneous receive calls to issue
- TurboTCPQElem qNotifyEntry; // notification queue entry
- TurboTCPQElem qDisposeEntry; // disposal queue entry
- TurboTCPQElem qActiveStreamEntry; // active stream list queue entry
-
- #if TurboTCP_CFM
- static UniversalProcPtr notifyProcUPP; // UPP for asynchronous notification proc
- #endif
-
- };
-